package com.fortune.rainshine.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * @author wcy
 * @description 无TokenAOP
 * @createDate 18-3-28
 * @updateBy
 * @updateDate
 */
@Aspect
@Component
public class NoTokenHttpAspect {
    /**
     * 打印日志 slf4j
     */
    private final static Logger logger= LoggerFactory.getLogger(NoTokenHttpAspect.class);


    @Pointcut("execution(public * com.fortune.rainshine.feign.system.VideoNewsFeign.*.*(..))")
    public void log(){

    }

    /**
     * 请求拦截
     * @param joinPoint
     */
    @Before("log()")
    public void dobefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        logger.info("[http_url] {}",request.getRequestURL());
        logger.info("[http_method] {}",request.getMethod());
        logger.info("[http_ip] {}",request.getRemoteAddr());
        logger.info("[class_method] {}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        logger.info("[http_args] {}",joinPoint.getArgs());
    }
    /**
     * 请求后记录行为等
     */
    @After("log()")
    public void doafter(){
    }

    /**
     * 返回后打印返回值
     * @param object
     */
    @AfterReturning(pointcut = "log()",returning = "object")
    public void doafterreturning(Object object){
        logger.info("【respone】={}",object.toString());
    }

}
